package Engine.Game;

/**
 * Animates the game so the objects can be moving around
 * @author Chronicles of Tuxedo dev team
 */
public class GameMotion implements Runnable {

    private GameDisplay game;
    private boolean isRunning;
    private Thread thread;
    /**
     * Number of frames per second of the game
     */
    public static final int FPS = 30;
    /*+
     * Delay time between cicles of painting
     */
    public static final int DELAY = 1000/FPS;

    /**
     * Creates a GameMotion object containing a reference to its Game container.
     * @param game the game container
     */
    public GameMotion(GameDisplay game) {
        this.game = game;
    }

    /**
     * Main cycle of the game
     */
    public void run() {
        isRunning = true;
        while (isRunning) {
            game.handleKeyboard();
            game.paint();
            try {
                Thread.sleep(DELAY);
            } catch (InterruptedException ex) { }
        }
    }

    /**
     * Starts the thread
     */
    public void play() {
        thread = new Thread(this);
        thread.start();
    }

    /**
     * Finishes the thread
     */
    public void stop() {
        isRunning = false;
        thread.interrupt();
    }
}
